package presentation;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import javax.swing.JComponent;
import javax.swing.TransferHandler;
import javax.swing.JList;

public class PPlotTransferHandler extends TransferHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public int getSourceActions(JComponent c) {
	    return MOVE;
	}

	public Transferable createTransferable(JComponent c) {
	    return new StringSelection((String)((JList) c).getSelectedValue());
	}

	public void exportDone(JComponent c, Transferable t, int action) {
	    if (action == MOVE) {
	        JList list= (JList)c;
	        list.clearSelection();
	    }
	}
	
	public boolean canImport(TransferHandler.TransferSupport info) {
        // Check for String flavor
		System.out.print("Checking" + info.getComponent() + "\n");
        if (info.getComponent() instanceof PGraphPanel) 
        {
            return true;
        }
        return false;
   }
	
	public boolean importData(TransferHandler.TransferSupport info) {
        if (!info.isDrop()) {
            return false;
        }
        
        PGraphPanel p = (PGraphPanel) info.getComponent();
        try
        {
        	p.changeGraph((String)info.getTransferable().getTransferData(DataFlavor.stringFlavor));
        }
        catch(Exception e)
        {
        	return false;
        }
        
        return true;
    }


	

}
